package com.glb.gilibuy.web.admin;

import com.glb.gilibuy.entity.Admin;
import com.glb.gilibuy.util.MapGenerator;
import com.glb.gilibuy.web.BaseController;
import com.glb.gilibuy.web.vo.AdminVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Author: Yupeng Zhang
 * @Date: 2021-08-09 14:52
 */
@Api(tags = "管理员模块")
@Controller
public class AdminController extends BaseController {
    @ApiOperation(value = "管理员登录", notes = "管理员登录", produces = "application/json")
    @PostMapping("/adminLoginIn")
    @ResponseBody
    public Map<String, Object> loginIn(@RequestParam("adminName") String adminName,
                                       @RequestParam("password") String password,
                                       HttpSession session) {
        if (StringUtils.isEmpty(adminName)) {
            return MapGenerator.getRes(500, "请输入管理员名！");
        }
        if (StringUtils.isEmpty(password)) {
            return MapGenerator.getRes(500, "请输入密码！");
        }
        if(adminService.checkAdmin(adminName, password) > 0){

            AdminVO adminVO = new AdminVO();

            Admin admin = adminService.selectAdminByAdminName(adminName);

            adminVO.setAdminId(admin.getAdminId());

            adminVO.setAdminName(admin.getAdminName());

            session.setAttribute("admin", adminVO );

            System.out.println("登录管理员:" + session.toString());

            return MapGenerator.getRes(200, "管理员成功登录！");
        }
        return MapGenerator.getRes(500, "管理员登录失败！");
    }

    @ApiOperation(value = "管理员添加", notes = "管理员添加", produces = "application/json")
    @PostMapping("/admin/addAdmin")
    @ResponseBody
    public Map<String, Object> addAdmin(@RequestParam("adminName") String adminName,
                                       @RequestParam("password") String password) {
        if (StringUtils.isEmpty(adminName)) {
            return MapGenerator.getRes(500, "请输入管理员名！");
        }
        if (StringUtils.isEmpty(password)) {
            return MapGenerator.getRes(500, "请输入密码！");
        }
        if(adminService.selectAdminByAdminName(adminName) != null){
            return MapGenerator.getRes(500, "管理员名已被使用！");
        }
        if(adminService.addAdmin(adminName, password) > 0){
            return MapGenerator.getRes(200, "管理员成功添加！");
        }
        return MapGenerator.getRes(500, "管理员添加失败！");
    }

    @ApiOperation(value = "管理员查看", notes = "管理员查看", produces = "application/json")
    @PostMapping("/admin/showAdmin")
    @ResponseBody
    public Map<String, Object> showAdmin(){
        List<Admin> adminList = adminService.findALLAdmin();
        if(adminList.isEmpty()){
            return MapGenerator.getRes(500, "没有查到管理员！");
        }
        Map<String, Object> data = new HashMap<>();
        data.put("adminList", adminList);
        return MapGenerator.getResWithData(200, "查看管理员信息成功！",data);
    }
//    public JSONUtil showAdmin(){
//        List<Admin> adminList = adminService.findALLAdmin();
//        if(adminList.isEmpty()){
//            return null;
//        }
//        JSONUtil<Admin> r = new JSONUtil<Admin>();
//        r.setCode(0);
//        r.setMsg("查看管理员信息成功！");
//        r.setData(adminList);
//        return r;
//    }

    @ApiOperation(value = "管理员冻结", notes = "管理员冻结", produces = "application/json")
    @PostMapping("/admin/lockAdmin")
    @ResponseBody
    public Map<String, Object> lockAdmin(@RequestParam("adminName")String adminName){
        Admin admin = adminService.selectAdminByAdminName(adminName);
        if(admin == null){
            return MapGenerator.getRes(500, "没有查到管理员！");
        }
        if(adminService.lockAdminByAdminId(admin.getAdminId()) > 0){
            return MapGenerator.getRes(200, "管理员冻结成功！");
        }
        return MapGenerator.getRes(500, "管理员冻结失败！");
    }

    @ApiOperation(value = "管理员解冻", notes = "管理员解冻", produces = "application/json")
    @PostMapping("/admin/unlockAdmin")
    @ResponseBody
    public Map<String, Object> unlockAdmin(@RequestParam("adminName")String adminName){
        Admin admin = adminService.selectAdminByAdminName(adminName);
        if(admin == null){
            return MapGenerator.getRes(500, "没有查到管理员！");
        }
        if(adminService.unlockAdminByAdminId(admin.getAdminId()) > 0){
            return MapGenerator.getRes(200, "管理员解冻成功！");
        }
        return MapGenerator.getRes(500, "管理员解冻失败！");
    }

}
